package com.cxt.config;


//import com.cxt.interceptor.JwtTokenAdminInterceptor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cxt.entity.User;
import com.cxt.interceptor.JwtTokenUserInterceptor;
import com.cxt.interceptor.PDInterceptor;
import com.cxt.json.JacksonObjectMapper;
import com.cxt.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.DigestUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;
import java.util.Random;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class webMvcConfiguration extends WebMvcConfigurationSupport {
    @Autowired
    private JwtTokenUserInterceptor jwtTokenAdminInterceptor;
    @Autowired
    private PDInterceptor pDInterceptor;
    @Autowired
    private IUserService userService;
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
////                .allowedOriginPatterns("*")
//                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
//                .allowCredentials(false).maxAge(1800);
//    }
    /**
     * root管理员帐号
     */
    private void initRoot() {
        User user = new User();
        LambdaQueryWrapper<User>  query = new LambdaQueryWrapper<>();
        query.eq(User::getPhone, "adminRoot");
        List<User> one = userService.list(query);
        if(one != null){
            //删除
            userService.remove(query);
        }
        user.setPhone("adminRoot");
        //随机6位数字
        Random random = new Random();
        int fourDigitRandomNumber = 100000 + random.nextInt(900000);
        String s = DigestUtils.md5DigestAsHex(Integer.valueOf(fourDigitRandomNumber).toString().getBytes());
        user.setPassword(s);
        user.setIsAdmin(true);
        user.setNickname("根管理员");
        boolean save = userService.save(user);
        log.info("----------------------系统root管理员----------------------------------");
        log.info("------------------------帐号:adminRoot------------------------");
        log.info("------------------------密码:"+fourDigitRandomNumber+"------------------------");
        log.info("--------------------------------------------------------------------");
    }
    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        initRoot();
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/user/register")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/sendCode")
                .excludePathPatterns("/user/sendCode*")
                .excludePathPatterns("/user/updatePassword");
        registry.addInterceptor(pDInterceptor)
                //拦截发起拼车信息
                .addPathPatterns("/carpoolInfo/addCarpoolInfo")
                .addPathPatterns("/carpoolInfo/addJoin");
    }

    /**
     * 扩展消息转换器
     */
    @Override
    //
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //设置消息转换器支持的媒体类型
        converter.setObjectMapper(new JacksonObjectMapper());
        //添加到消息转换器列表
        converters.add(0, converter);
    }

    /**
     * 设置静态资源映射
     *
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始设置静态资源映射...");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
